package com.dianrui.moonfire.activity;

import android.annotation.SuppressLint;
import android.os.Build;
import android.os.Handler;
import android.support.annotation.RequiresApi;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.blankj.utilcode.util.SPUtils;
import com.dianrui.moonfire.Constant;
import com.dianrui.moonfire.R;
import com.dianrui.moonfire.util.NoDoubleClickUtils;
import com.dianrui.moonfire.util.Url;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Description:优惠劵的webview
 * User: luoziyin
 * Date: 2020-8-17
 */
@SuppressWarnings("ALL")
public class CouponWebviewActicity extends BaseActivity {
    private boolean isSuccess = false;
    private boolean isError = false;
    private LinearLayout ll_control_error;

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.webview)
    WebView couponWebview;
    @BindView(R.id.act_progressbar)
    ProgressBar mProgressBar;

    @BindView(R.id.show_webview_layout)
    LinearLayout showWebviewLayout;

    @Override
    public int getContentViewId() {
        return R.layout.webview;
    }
    @SuppressLint("SetJavaScriptEnabled")
    @Override
    public void initView() {
        customInit(true);
        couponWebview.post(new Runnable() {
            @Override
            public void run() {
                String showCouponUrl = Url.BASE_URL + Constant.my_coupon_url+ SPUtils.getInstance().getString("member_id");
                couponWebview.loadUrl(showCouponUrl);

                WebSettings webSetting = couponWebview.getSettings();
                webSetting.setJavaScriptEnabled(true);//支持js
                webSetting.setUseWideViewPort(true);
                webSetting.setLoadWithOverviewMode(true);
                webSetting.setAppCacheEnabled(false);
                webSetting.setSupportZoom(true);
                webSetting.setBuiltInZoomControls(true);
                webSetting.setDisplayZoomControls(false);
                webSetting.setDomStorageEnabled(true);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    webSetting.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
                }
                couponWebview.setWebChromeClient(new couponChromeClient());
                couponWebview.setWebViewClient(new couponWebViewClient());

            }
        });
        new Handler().post(new Runnable() {
            @Override
            public void run() {
                couponWebview.removeView(couponWebview);
            }
        });

        ll_control_error = (LinearLayout) findViewById(R.id.ll_control_error);
        Button errorBtn=ll_control_error.findViewById(R.id.online_error_btn_retry);
        TextView errorTitle=ll_control_error.findViewById(R.id.title);
        ImageView backimg=ll_control_error.findViewById(R.id.back);
        errorTitle.setText("找不到网页");
        errorBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ll_control_error.setVisibility(View.GONE);
                showWebviewLayout.setVisibility(View.VISIBLE);
                couponWebview.reload();
            }
        });
        backimg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    @Override
    public void initData() {}


    //自定义一个WebChromeClient
    class couponChromeClient extends WebChromeClient {
        @Override
        public void onReceivedTitle(WebView view, String titles) {
            super.onReceivedTitle(view, titles);
            if (titles != null && !isFinishing()) {
                title.setText(titles);//获取网页标题
            }
        }
    }

    //自定义一个WebClient
    class couponWebViewClient extends WebViewClient {


        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            if (!isError) {
                isSuccess = true;
                //回调成功后的相关操作
                ll_control_error.setVisibility(View.GONE);
                showWebviewLayout.setVisibility(View.VISIBLE);
                couponWebview.setVisibility(View.VISIBLE);
            } else {
                isError = false;
                couponWebview.setVisibility(View.GONE);
                showWebviewLayout.setVisibility(View.GONE);
                ll_control_error.setVisibility(View.VISIBLE);
            }

        }

        //加载错误的时候会回调
        @RequiresApi(api = Build.VERSION_CODES.M)
        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            super.onReceivedError(view, errorCode, description, failingUrl);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                return;
            }
            isError = true;
            isSuccess = false;
            //6.0以下执行
            couponWebview.setVisibility(View.GONE);
            showWebviewLayout.setVisibility(View.GONE);
            ll_control_error.setVisibility(View.VISIBLE);

        }


        @Override
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
            super.onReceivedError(view, request, error);
            isError = true;
            isSuccess = false;
            //6.0以上执行
            couponWebview.setVisibility(View.GONE);
            showWebviewLayout.setVisibility(View.GONE);
            ll_control_error.setVisibility(View.VISIBLE);

        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }


    @OnClick({R.id.back})
    public void onViewClicked(View view) {
        if (NoDoubleClickUtils.isDoubleClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.back:
                if (couponWebview.canGoBack()) {
                    couponWebview.goBack();
                }else {
                    onBackPressed();
                }
                break;
            default:
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (couponWebview != null) {
            couponWebview.onResume();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (couponWebview != null) {
            couponWebview.onPause();
        }
    }

    @Override
    public void onBackPressed() {
        if (couponWebview.canGoBack()) {
            couponWebview.goBack();
        }else {
            finish();
        }
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && couponWebview.canGoBack()) {
            couponWebview.goBack();
            return true;
        } else {
            onBackPressed();
        }
        return super.onKeyDown(keyCode, event);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (couponWebview != null) {
            couponWebview.clearCache(true);
            couponWebview.stopLoading();
            couponWebview.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (couponWebview != null) {
                        couponWebview.destroy();
                        couponWebview = null;
                    }
                }
            }, 3000);
        }
    }
}
